package com.mqz.zebraEcommerce.commodity.service.impl;

import com.mqz.zebraEcommerce.commodity.dao.SpuInfoDao;
import com.mqz.zebraEcommerce.commodity.entity.SpuInfoEntity;
import com.mqz.zebraEcommerce.commodity.vo.SearchResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mqz.zebraEcommerce.common.utils.PageUtils;
import com.mqz.zebraEcommerce.common.utils.Query;

import com.mqz.zebraEcommerce.commodity.dao.SkuInfoDao;
import com.mqz.zebraEcommerce.commodity.entity.SkuInfoEntity;
import com.mqz.zebraEcommerce.commodity.service.SkuInfoService;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        String catalogId = (String) params.get("catalogId");
        String brandId = (String) params.get("brandId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");

        if (StringUtils.isNotBlank(key)) {
            wrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        if (StringUtils.isNotBlank(catalogId) && !catalogId.equalsIgnoreCase("0")) {
            wrapper.eq("catalog_id", catalogId);
        }

        if (StringUtils.isNotBlank(brandId) && !brandId.equalsIgnoreCase("0")) {
            wrapper.eq("brand_id", brandId);
        }

        if (StringUtils.isNotBlank(min)) {
            wrapper.ge("price", min);
        }

        if (StringUtils.isNotBlank(max)) {
            wrapper.le("price", max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Resource
    private SpuInfoDao spuInfoDao;

    @Override
    public SearchResultVO searchPage(Map<String, Object> params) {

        String keyword = (String) params.get("keyword");
        String catalog3Id = (String) params.get("catalog3Id");
        String brandId = (String) params.get("brandId");

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        //筛选上架商品
        List<SpuInfoEntity> spuInfoEntities = spuInfoDao.selectList(new QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
        List<Long> spuInfoEntityIds = spuInfoEntities.stream().map(item -> item.getId()).collect(Collectors.toList());

        if (spuInfoEntityIds != null && spuInfoEntityIds.size() > 0) {
            wrapper.in("spu_id", spuInfoEntityIds);
        } else {
            SearchResultVO searchResultVO = new SearchResultVO();
            searchResultVO.setPageNum(1);
            searchResultVO.setTotal(0L);
            searchResultVO.setTotalPages(0);
            searchResultVO.setPageNavs(new ArrayList<>());
            searchResultVO.setCommodity(new ArrayList<>());
            searchResultVO.setKeyword(keyword == null ? "" : keyword);
            searchResultVO.setTitle("商品搜索 - 斑马");
            return searchResultVO;
        }


        if (StringUtils.isNotBlank(keyword)) {
            wrapper.and(w -> {
                w.eq("sku_id", keyword).or().like("sku_name", keyword);
            });
        }

        if (StringUtils.isNotBlank(catalog3Id) && !catalog3Id.equalsIgnoreCase("0")) {
            wrapper.eq("catalog_id", catalog3Id);
        }

        if (StringUtils.isNotBlank(brandId) && !brandId.equalsIgnoreCase("0")) {
            wrapper.eq("brandId", brandId);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);

        SearchResultVO searchResultVO = new SearchResultVO();
        searchResultVO.setCommodity((List<SkuInfoEntity>) pageUtils.getList());
        searchResultVO.setPageNum(pageUtils.getCurrPage());
        searchResultVO.setTotal((long) pageUtils.getTotalCount());

        int totalPage = pageUtils.getTotalPage();
        searchResultVO.setTotalPages(totalPage);

        List<Integer> pageNavs = new ArrayList<>();
        for (int i = 1; i <= totalPage; i++) {
            pageNavs.add(i);
        }
        searchResultVO.setPageNavs(pageNavs);
        searchResultVO.setKeyword(keyword == null ? "" : keyword);
        if (StringUtils.isNotBlank(keyword)){
            searchResultVO.setTitle(keyword + " - 商品搜索 - 斑马");
        } else {
            searchResultVO.setTitle("商品搜索 - 斑马");
        }

        return searchResultVO;
    }
}